-- || Created by Revelse

LobbyChoose = inherit(Singleton)

addEvent("reciveGamemodeDetails", true)
addEvent("hideLobbyChoose", true)
addEvent("showLobbyChoose", true)

function LobbyChoose:constructor()
	self.m_GamemodeDetails = {}
	self.m_RenderTarget = dxCreateRenderTarget(500,200,true)
	self.m_ScreenSource = dxCreateScreenSource(screenWidth,screenHeight)
	self.m_Shader = dxCreateShader("files/shader/blurshader.fx")
	self:requestLobbyFiles()
	self.m_Active = false
	self.m_Hover = false
	self.m_AngelGap = 0
	self.m_Mul = 1
	self.m_BlurStrength = 10
	self.m_Progress = 0
	
	self.m_StartTick = false
	self.m_EndTick = false
	
	self.m_Rotation = 0
	
	self:show()
	
	addEventHandler("hideLobbyChoose", root, bind(self.hide,self))
	addEventHandler("showLobbyChoose", root, bind(self.show,self))
	addEventHandler("reciveGamemodeDetails", root, bind(self.reciveDetails,self))
	addEventHandler("onClientRender", root, bind(self.draw,self))
	addEventHandler("onClientKey", root, bind(self.onKey,self))
	addEventHandler("onClientClick", root, bind(self.onClick,self))
	
	setTimer( function ()
		self:requestLobbyFiles()
	end, 1000, 0)
end

function LobbyChoose:onClick(btn,std)
	if btn == "left" and std == "down" and self.m_Active and self.m_Hover then -- DEV JOIN
		local angel = (self.m_Rotation+(self.m_AngelGap*self.m_Hover))/180*math.pi
		local x,y = math.floor(screenWidth/2+(math.cos(angel)*screenHeight/4)-(128/2)),math.floor(screenHeight/2+(math.sin(angel)*screenHeight/4)-(128/2))
		if isCursorOverArea(x+128,y,200,128) then
			local cy = select(2,getAbsoluteCursorPosition())
			local room = math.ceil((cy-y)/(128/4))
			if self:getRooms()[room] then
				localPlayer:triggerEvent("requestGamemodeJoin",self:getName(),room)
			end
		end
	end
end

function LobbyChoose:onKey(key,std)
	--[[if key == "e" and not std and not isChatBoxInputActive() and self.m_Hover then -- DEV JOIN
		localPlayer:triggerEvent("requestGamemodeJoin",self:getName(),1) .]]
	if key == "a" and std and not self.m_Hover then
		self.m_Mul = -50
	elseif key == "d" and std and not self.m_Hover then
		self.m_Mul = 50
	elseif key == "a" or key == "d" and not std and not self.m_Hover then
		self.m_Mul = 1
	end
end

function LobbyChoose:show()
	showCursor(true)
	setCameraMatrix(-1957.9395751953,484.37411499023,34.703098297119,-1958.8000488281,483.96673583984,35.009048461914,0,70)
	setCameraInterior(0)
	self.m_Active = true
end

function LobbyChoose:hide()
	showCursor(false)
	self.m_Active = false
end

function LobbyChoose:getName() return self.m_GamemodeDetails[self.m_Hover].lobbyName end
function LobbyChoose:getRooms() return self.m_GamemodeDetails[self.m_Hover].rooms end

function LobbyChoose:draw()
	if #self.m_GamemodeDetails == 0 or not self.m_Active then
		return
	end

	local hover = false
	local calcProgress
	
	if self.m_Hover then
		calcProgress = (getTickCount()-self.m_StartTick)/(self.m_EndTick-self.m_StartTick)
		self.m_Progress = calcProgress < 1 and calcProgress or 1
	else
		if self.m_Progress - 0.01 < 0 then
			self.m_Progress = 0
		else
			self.m_Progress = self.m_Progress - 0.01
		end
	end
	
	dxUpdateScreenSource(self.m_ScreenSource)
    dxSetShaderValue(self.m_Shader, "ScreenSource", self.m_ScreenSource)
    dxSetShaderValue(self.m_Shader, "BlurStrength", self.m_BlurStrength+(5*self.m_Progress))
	dxSetShaderValue(self.m_Shader, "UVSize", screenWidth, screenHeight)
	
	dxDrawImage(0,0,screenWidth,screenHeight,self.m_Shader)
	dxDrawImage(screenWidth/2-(150/2),screenHeight/2-(150/2),150,150,"files/images/Logo.png")
	dxDrawCircle(screenWidth/2,screenHeight/2,screenHeight/4,2,0.1,tocolor(0,0,0,20))
	
	for key, value in ipairs(self.m_GamemodeDetails) do

		
		
		local angel = (self.m_Rotation+(self.m_AngelGap*key))/180*math.pi
		
		local x,y = math.floor(screenWidth/2+(math.cos(angel)*screenHeight/4)-(128/2)),math.floor(screenHeight/2+(math.sin(angel)*screenHeight/4)-(128/2))
	
		
		
		--dxDrawLine(screenWidth/2,screenHeight/2,x+125/2,y+125/2, tocolor(0,0,0,50), 2, false)
		
		if fileExists(("files/images/lobby/%s.png"):format(value.lobbyName)) then
			dxDrawImage(x,y,128,128,("files/images/lobby/%s.png"):format(value.lobbyName))
		else
			dxDrawRectangle(x,y,128,128,tocolor(0,255,0))
		end	
		
		
		if isCursorOverArea(x,y,128,128) and not hover then
			hover = true
			self.m_Mul = 0.5
		elseif self.m_Hover and key == self.m_Hover and isCursorOverArea(x,y,128+200,128) then
			hover = true
			self.m_Mul = 0.5
		end
		if not self.m_Hover and hover then
			self.m_Hover = key
			self.m_StartTick = getTickCount()
			self.m_EndTick = self.m_StartTick + 250
		end
	end

	if not hover and self.m_Hover then
		self.m_Mul = 1
		self.m_Hover = false
	end
	
		if self.m_Hover then
			
			local angel = (self.m_Rotation+(self.m_AngelGap*self.m_Hover))/180*math.pi
		
			local x,y = math.floor(screenWidth/2+(math.cos(angel)*screenHeight/4)-(128/2)),math.floor(screenHeight/2+(math.sin(angel)*screenHeight/4)-(128/2))
			
			calcProgress = (getTickCount()-self.m_StartTick)/(self.m_EndTick-self.m_StartTick)
			progress = calcProgress < 1 and calcProgress or 1
			
			for i = 0, 3, 1 do
				local index = i + 1
				if self:getRooms()[index] then
					if not isCursorOverArea(x+128,y+i*128/4,200,128/4) then
						dxDrawRectangle(x+128,y+i*128/4,200*progress,128/4,i%2 == 0 and tocolor(100,100,100,200) or tocolor(200,200,200,200))
					else
						dxDrawRectangle(x+128,y+i*128/4,200*progress,128/4,tocolor(125,0,0,200))
					end
					if progress == 1 then
						local playerString = ("Raum %i - %s / %s"):format(index,self:getRooms()[index].currentPlayers,self:getRooms()[index].maxPlayers)
						dxDrawText(playerString,x+(200/2)-(dxGetTextWidth(playerString,1,"default-bold")/2)+128,y+(i*128/4)+5,0,0,tocolor(255,255,255),1,"default-bold")
					end
				end
			end
		end

	
		self.m_Rotation = self.m_Rotation + (0.10*self.m_Mul)
	dxDrawImage(screenWidth/2-(500/2),screenHeight/2-(200/2),500,200,self.m_RenderTarget)
	
	
	
end

function LobbyChoose:reciveDetails(details)
	self.m_AngelGap = 360/#details
	self.m_GamemodeDetails = details
end

function LobbyChoose:requestLobbyFiles()
	localPlayer:triggerEvent("requestGamemodeDetails")
end